/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "robotmanager.h"
#include "robotengine.h"
#include <QThread>
#include <QDebug>


RobotManager::RobotManager()
{

    versionInfo.componentName="RobotManager";
    versionInfo.componentVersion="V1.0";
    versionInfo.publishDate="2018.10.27";
}

RobotManager::~RobotManager(){
    RobotEngine* p_robot;
    foreach(p_robot, robotsMap){
        if(NULL!=p_robot)
        {
            delete p_robot;
            p_robot = NULL;
        }
    }
}

int RobotManager::readVersion(std::vector <ComponentVersion> &componentVersionList)
{

    componentVersionList.push_back(versionInfo);

    if(true==robotsMap.contains(1))
    {
        robotsMap.value(1)->readVersion(componentVersionList);
    }
    else
    {
        return -1;
    }
    return 1;
}

int RobotManager::stopAllRobot()
{
    QList<int> keys= robotsMap.keys();
    for(int i=0;i<keys.size(); i++ )
    {
        int robId = keys.at(i);
        robotsMap.value( robId )->resetProgram();

    }
}

int RobotManager::ceaseAllRobot()
{
    QList<int> keys= robotsMap.keys();
    for(int i=0;i<keys.size(); i++ )
    {
        int robId = keys.at(i);
        robotsMap.value( robId )->ceaseProgram();
    }
}

bool RobotManager::readRobotsXml( QString xmlfile )
{
    bool ok=openXml( xmlfile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        //消息预留
        //打开ｘｍｌ文件失败
        qDebug()<<"ERROR: openXml :"<<ok<< xmlfile;
        return false;
    }

    QDomNodeList nodelist = m_root.childNodes();
    int robotsCount = nodelist.size();

    if(robotsCount < 1)
    {
        //消息预留
        //没有相应的机器人信息
        qDebug()<<"robots count<1 !";
        closeXml();
        return false;
    }

    robotsInfoMap.clear();
    robotsMap.clear();
    robotsFolderMap.clear();
    for(int i = 0; i < robotsCount; i++ )
    {
        QDomNode robNode = nodelist.at(i);
        if( robNode.nodeName() .indexOf("#comment")>=0){
            continue;
        }

        if( !robNode.isNull()  )
        {

            int robID = robNode.toElement().attribute( "id" ).toUInt();
            QString folder = robNode.toElement().attribute( "folder" );
            robotsFolderMap.insert(robID, folder);

            RobotEngine* robot=new RobotEngine();
            robotsMap.insert( robID, robot );

             QString xmlfile = D_TCRCONTROLLER_DATA_PATH+ folder +"/config/"+D_ROBOT_INFO_FILE_NAME;
             //需要先给 robotId ，robotFolder  属性
             robot->robotInfo.robotId = robID;
             robot->robotInfo.robotFolder = folder;
             //这里面没 robotId ，robotFolder 属性
#if MD5CHECK_DEBUG
             if(md5Instance->compareMd5(xmlfile,robID))
             {
                 robot->readRobotInfoXml(xmlfile );
             }
             else
             {

             }
#else
             robot->readRobotInfoXml(xmlfile );
#endif
             robotsInfoMap.insert( robID, &robot->robotInfo );
             qDebug()<<" readRobotsXml======  "<< robID<< folder ;

        }else{
            ok=false;
            qDebug()<<" read  xml error!"<<xmlfile;
            break;
        }
    }

    closeXml();

    return ok?true:false;
}

int RobotManager::initRobots(int startType)
{
    QString  tmpFilePath=D_TCRCONTROLLER_DATA_PATH;
    readRobotsXml( tmpFilePath+GLOBAL_ROBOT_CONFIG );

  //  QMap<int , QThread*> robotsThreadMap;
    QList<int> keylist= robotsFolderMap.keys();
    for( int i=0; i<keylist.size(); i++ )
    {
         QThread* robthread = new QThread();
        robthread->start();
        while(!robthread->isRunning()){
            usleep(10);
        }

       int key = keylist.at(i);
       RobotEngine* robot = robotsMap.value( key  );
       robot->init(startType);

       robot->moveToThread(robthread);
       robotsThreadMap.insert( key, robthread );
    }

    return 1;

 }
